#include <Arduino.h>
#include "XL9555.h"
#include "Timer.h"
#include "OLED.h"
#include "LCD.h"
#include "AP3216C.h"
#define LED_PIN 1
#define BOOT 0

#define LED_TURN() digitalWrite(LED_PIN,!digitalRead(LED_PIN));
bool flag=0;
uint16_t als=0, ps=0, ir=0;
void tim0_ISR(void)
{
    LED_TURN();
    flag=1;
}

void setup()
{
    pinMode(LED_PIN,OUTPUT);
    lcd_init();
    ap3216c_init();
    xl9555_init();
    xl9555_io_config(AP_INT,IO_SET_INPUT);
    ap3216c_init_interrupt(PS_INT);
    ap3216c_set_int_clear_mode(MANUAL);
    ap3216c_set_ps_threshold(1023,100);
    timex_Init(TIM0_INT,5000,(uint16_t)8000,tim0_ISR);
}

void loop()
{
    if(flag)
    {
        ap3216c_read_data(&als, &ps, &ir);
        flag=false;
    }

    lcd_printf(1,1,LCD_FONT_32,"als:%5d",als);
    lcd_printf(2,1,LCD_FONT_32,"ps : %4d",ps);
    lcd_printf(3,1,LCD_FONT_32,"ir : %4d",ir);
    lcd_printf(4,1,LCD_FONT_32,"%d",xl9555_get_pin(AP_INT));
}
